package ref;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 * Author:ZouHeng
 * Description:
 * Date:2021-07-05  15:45
 **/
public class RefTest {
    @Test
    public void testRef() {
        //构造器引用：它的语法是Class::new，或者更一般的Class<T>::new
        final Car car = Car.create( Car::new );
        final List<Car> cars = Arrays.asList(car );

        //静态方法引用：它的语法是Class::static_method
        cars.forEach( Car::collide );

        //特定类的任意对象的方法引用：它的语法是Class::method
        cars.forEach( Car::repair );

        //特定对象的方法引用：它的语法是instance::method
        final Car police = Car.create( Car::new );
        cars.forEach( police::follow );
    }

    @Test
    public void testPersonRef() {
        Person[] persons = new Person[5];
        persons[0] = new Person("a", 5);
        persons[1] = new Person("b", 3);
        persons[2] = new Person("c", 4);
        persons[3] = new Person("d", 2);
        persons[4] = new Person("e", 1);

        //静态方法引用
        Arrays.sort(persons, (a, b) -> Person.compareByAge(a, b));
        Arrays.sort(persons, Person::compareByAge);
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);
        }

        //实例方法引用
        Person person = new Person("kobe", 38);
        Supplier<String> supplier = () -> person.getName();
        System.out.println("Lambda表达式输出结果：" + supplier.get());
        Supplier<String> supplier1 = person::getName;
        System.out.println("Lambda表达式输出结果：" + supplier1.get());

        //对象方法引用

        //构造方法引用
        Supplier<Person> supplier2 = Person::new;
    }
}
